import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RuleExecutor {

    private Map<Integer, List<BaseRule>> map = new HashMap<>();

    private static Integer AND = 1;

    private static Integer OR = 0;

    public RuleExecutor and(List<BaseRule> rules) {
        map.put(AND, rules);
        return this;
    }


    public RuleExecutor or(List<BaseRule> rules) {
        map.put(OR, rules);
        return this;
    }


    public Boolean executeRule(RuleData data) {
        for (Map.Entry<Integer, List<BaseRule>> entry : map.entrySet()) {
            Integer key = entry.getKey();
            List<BaseRule> value = entry.getValue();
            switch (key) {
                case 1:
                    return andValidate(data, value);

                case 0:
                    return orValidate(data, value);
                default:
                    break;
            }
        }

        return true;
    }

    private Boolean orValidate(RuleData data, List<BaseRule> values) {

        for (BaseRule rule : values) {

            if (rule.execute()) {
                return true;
            }
        }

        return false;
    }

    private Boolean andValidate(RuleData data, List<BaseRule> values) {
        for (BaseRule rule : values) {

            if(!rule.execute()){
                return false;
            }
        }

        return true;
    }
}
